package com.config.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QuartzConfig {

    @Bean
    public JobDetail DatabaseJob() {
        return JobBuilder.newJob(QuartzDatabase.class)
                .withIdentity("myJob")
                .storeDurably()
                .build();
    }

    @Bean
    public Trigger DatabaseTrigger() {
        return TriggerBuilder.newTrigger()
                .forJob(DatabaseJob())
                .withIdentity("myTrigger")
                .withSchedule(CronScheduleBuilder.cronSchedule("0 0 0 * * ?"))//每天晚上12点执行一次
                .build();
    }

    @Bean
    public Scheduler scheduler() throws Exception {
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.scheduleJob(DatabaseJob(), DatabaseTrigger());
        return scheduler;
    }

}
